package com.example.week2_try2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity5 extends AppCompatActivity {

    private static final String TAG = "MainActivity5";

    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        GridView mylist = findViewById(R.id.mylist1);

        handler=new Handler(Looper.myLooper()){//
            @Override
            public void handleMessage(@NonNull Message msg) {
                Log.d(TAG, "handleMessage: 收到消息");
                if(msg.what==9){
                    ArrayList<Item> listItems = (ArrayList<Item>) msg.obj;
                    MyAdapter myAdapter = new MyAdapter(MainActivity5.this,R.layout.activity_main6,listItems);
                    mylist.setAdapter(myAdapter);
//                    mylist.setEmptyView(findViewById(R.id.nodata));

                    //切换显示状态

                    mylist.setVisibility(View.VISIBLE);
                    //提取数据
//                    List<String> list2 = (List<String>) msg.obj;
//                    ListAdapter adapter = new ArrayAdapter<String>(MainActivity5.this,
//                            android.R.layout.simple_list_item_1,list2);//list_data可换list1
//                    setListAdapter(adapter);
//                    String[] list_data = {"one","two","three","four"};
//                    ListAdapter adapter = new ArrayAdapter<String>(MainActivity5.this, android.R.layout.simple_list_item_1,list_data);//list_data可换list1
//                    mylist.setAdapter(adapter);

                }
                super.handleMessage(msg);
            }
        };

        RateItemTask myTask = new RateItemTask();
        myTask.setHandler(handler);    //不调用该方法会显示空指针异常
        Thread thread = new Thread(myTask);
        thread.start();


    }
}